﻿// ===================================
// <copyright>LogoUI Co.</copyright>
// <author>Vlad Spivak</author>
// <email>mailto:vlads@logoui.co.il</email>
// <created>21/00/10</created>
// <lastedit>Sunday, November 21, 2010</lastedit>

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the 'Software'), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//
// <remarks>Part of this software based on various internet sources, mostly on the works
// of members of Wpf Disciples group http://wpfdisciples.wordpress.com/
// Also project may contain code from the frameworks: 
//        Nito 
//        OpenLightGroup
//        nRoute
// </remarks>
// ====================================================================================//


using System;
using System.Windows;
using System.Windows.Interactivity;

namespace LogoFX.Infra.Interactivity
{
    /// <summary>
    /// Makes object invisible when some value became less than provided settings
    /// </summary>
    [Obsolete("Use Microsoft.Expression.Interactivity.Core.DataTrigger if possible")]
    public class LessToVisibilityBeaviour:Behavior<FrameworkElement>
    {

        #region Source dependency property

        public double Source
        {
            get { return (double) GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }

        public static readonly DependencyProperty SourceProperty =
            DependencyProperty.Register("Source", typeof (double), typeof (LessToVisibilityBeaviour), new PropertyMetadata(default(double), OnSourceChanged));

        private static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            LessToVisibilityBeaviour ltvb = (LessToVisibilityBeaviour) d;
            if (ltvb.AssociatedObject == null)
                return;
            ltvb.ApplyVisibility();
        }

        #endregion
        #region Value dependency property
        public double Value
        {
            get { return (double) GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof (double), typeof (LessToVisibilityBeaviour), new PropertyMetadata(0.0, OnValueChanged));

        private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            LessToVisibilityBeaviour ltvb = (LessToVisibilityBeaviour)d;
            if (ltvb.AssociatedObject == null)
                return;
            ltvb.ApplyVisibility();
        }

        #endregion


        public bool IsRevert
        {
            get { return (bool)GetValue(IsRevertProperty); }
            set { SetValue(IsRevertProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsRevert.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsRevertProperty =
            DependencyProperty.Register("IsRevert", typeof(bool), typeof(LessToVisibilityBeaviour), new PropertyMetadata(false));

        
        protected override void OnAttached()
        {
            ApplyVisibility();
            base.OnAttached();
        }

        private void ApplyVisibility()
        {
            if (IsRevert == false)
            {
                if (Value > Source)
                {
                    AssociatedObject.Visibility = Visibility.Collapsed;
                }
                else
                {
                    AssociatedObject.Visibility = Visibility.Visible;
                }
            }
            else
            {
                if (Value > Source)
                {
                    AssociatedObject.Visibility = Visibility.Visible;
                }
                else
                {
                    AssociatedObject.Visibility = Visibility.Collapsed;
                }
            }

        }
    }
}
